<html>
<head>
<title>Crypto Utilities Test Page</title>
<script src="namespace.js"></script>
<script src="base.js"></script>
<script src="mersene_twister.js"></script>
<script src="random.js"></script>
<script src="crypto.js"></script>
<script src="enigma.js"></script>
<script src="vector.js"></script>
<script src="timer.js"></script>
<script src="unit.js"></script>
</head>
<body>
<h1><script>document.write(document.title);</script></h1>
<SCRIPT>
var UT = global_namespace.Import('startpad.unit'); 
var ts = new UT.TestSuite();
ts.DWOutputDiv();
</script>
<script>
var Crypto = global_namespace.Import('startpad.crypto');
var Random = global_namespace.Import('startpad.random');
var Enigma = global_namespace.Import('startpad.enigma');
var Vector = global_namespace.Import('startpad.vector');

ts.AddTest("Entropy calculation", function(ut)
{
	aTests = [
		['', 0],
		['a', 0],
		['aaaa', 0],
		['ab', 1],
		['abcd', 2],
		['aA', 1]
		];

	var e = new Crypto.Entropy();
	for (var i = 0; i < aTests.length; i++)
		{
		e.Init().AddString(aTests[i][0]);
		ut.AssertEq(e.BitsPerChar(), aTests[i][1], 'String: ' + aTests[i][0]);
		}
		
	e = new Crypto.Entropy('', Crypto.Entropy.AlphaOnly);
	e.AddString('aA');
	ut.AssertEq(e.BitsPerChar(), 0);
});

ts.AddTest("Enigma Entropy Test", function(ut)
{
	var machine = new Enigma.Enigma();
	var sPlain = "This is the test message - it should have a low entropy compared to ramdom text.";
	var e = new Crypto.Entropy(sPlain, Crypto.Entropy.AlphaOnly);
	var bitsPlain = e.BitsPerChar();
	
	var c = 1000;
	for (var i = 0; i < c; i++)
		{
		var posLast = machine.position.concat();
		var cipher = machine.Encode(sPlain);
		var bits = e.Init().AddString(cipher).BitsPerChar();
		ut.AssertLT(bitsPlain, bits);
		machine.position = posLast;
		machine.IncrementRotors();
		}
});

ts.Run();
ts.Report();

</script>
</body>
</html>
